package com.code.zg.news.presenters.presenter;

import com.code.zg.news.base.RxBasePresenter;
import com.code.zg.news.model.bean.NightMode;
import com.code.zg.news.model.http.RetrofitHelper;
import com.code.zg.news.presenters.contracts.MainContract;
import com.code.zg.news.utils.RxBus;
import com.code.zg.news.utils.RxUtils;
import com.tbruyelle.rxpermissions.RxPermissions;

import javax.inject.Inject;

import rx.Observer;
import rx.Subscription;
import rx.functions.Func1;

/**
 * Created by zhanggang on 2017/2/22.
 */

public class MainPresenter extends RxBasePresenter<MainContract.View> implements MainContract.Presenter{
    private final RetrofitHelper retrofitHelper;

    @Inject
    public MainPresenter(RetrofitHelper retrofitHelper){
        this.retrofitHelper=retrofitHelper;
        registEvent();
    }

    private void registEvent() {
        Subscription subscription = RxBus.getDefault().toObservable(NightMode.class)
                .compose(RxUtils.<NightMode>rxSchedulesHelper())
                .map(new Func1<NightMode, Boolean>() {
                    @Override
                    public Boolean call(NightMode nightMode) {
                        return nightMode.nightMode;
                    }
                })
                .subscribe(new Observer<Boolean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.showError("switch failed");
                    }

                    @Override
                    public void onNext(Boolean aBoolean) {
                        mView.useNightMode(aBoolean);
                    }
                });
        addSubscribe(subscription);
    }

    @Override
    public void checkVersion(String currentVersion) {

    }

    @Override
    public void checkPermissions(RxPermissions rxPermissions) {

    }
}
